package com.tjp.eight;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.tjp.eight.comment.AppManager;
import com.tjp.eight.ui.fragment.tabFind.FragmentFind;
import com.tjp.eight.ui.fragment.tabhome.FragmentHome;
import com.tjp.eight.ui.fragment.tabme.FragmentMe;
import com.tjp.eight.ui.fragment.tabvideo.FragmentVideo;
import com.tjp.eight.utils.AppUtils;
import com.tjp.eight.utils.ToatUtils;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

public class MainActivity extends AppCompatActivity {
    private static final int TAG_HOME = 0x01;
    private static final int TAG_MOVIE = 0x02;
    private static final int TAG_FOUND = 0x03;
    private static final int TAG_ME = 0x04;
    private Intent addFragmentIntent = null; // 解决java.lang.IllegalStateException:

    @Bind(R.id.rb_home)
    RadioButton rb_home;
    @Bind(R.id.rb_video)
    RadioButton rb_video;
    @Bind(R.id.rb_find)
    RadioButton rb_find;
    @Bind(R.id.rb_me)
    RadioButton rb_me;

    private FragmentHome fragmentHome;
    private FragmentVideo fragmentVideo;
    private FragmentFind fragmentFind;
    private FragmentMe fragmentMe;
    private String currentTag = "";
    private long exitTime;
    private long videoTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addFragmentIntent = getIntent();
        AppManager.getAppManager().AddActivity(this);

        ButterKnife.bind(this);
        AppUtils.check(this);
        fragmentHome = new FragmentHome();
        fragmentVideo = new FragmentVideo();
        fragmentFind = new FragmentFind();
        fragmentMe = new FragmentMe();

        rb_home.setChecked(true);
        changeFragment(fragmentHome);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        addFragmentIntent = intent;
    }

    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        // super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        AppManager.getAppManager().finishActivity(this);
        RxBus.getInstance().clear();
    }

    @Override
    protected void onResume() {
        super.onResume();
        resultIntent(addFragmentIntent);
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
        MobclickAgent.onPause(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!JCVideoPlayer.backPress()) {
                if (System.currentTimeMillis() - exitTime > 2000) {
                    exitTime = System.currentTimeMillis();
                    ToatUtils.toastSuccess(this, "再按一次，提出程序");
                } else {
                    AppManager.getAppManager().finishActivity(this);
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnCheckedChanged({R.id.rb_home, R.id.rb_video, R.id.rb_find, R.id.rb_me})
    void initTabEvent(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            switch (buttonView.getId()) {
                case R.id.rb_home:
                    onShowHome();
                    break;
                case R.id.rb_video:
                    onShowVideo();
                    break;
                case R.id.rb_find:
                    onShowFind();
                    break;
                case R.id.rb_me:
                    onShowMe();
                    break;
            }
        }
    }

    @OnClick({R.id.rb_home, R.id.rb_video, R.id.rb_find, R.id.rb_me})
    void onClick(View buttonView) {
        switch (buttonView.getId()) {
            case R.id.rb_home:
                //onShowHome();
                break;
            case R.id.rb_video:
                //onShowVideo();
                if (fragmentVideo.getClass().getCanonicalName().equals(getCurrentTag())) {
                    if (System.currentTimeMillis() - videoTime <= 500) {
                        fragmentVideo.scrollTop();
                    } else {
                        videoTime = System.currentTimeMillis();
                    }
                }
                break;
            case R.id.rb_find:
                //onShowFind();
                break;
            case R.id.rb_me:
                //onShowMe();
                break;
        }
    }

    void onShowHome() {
        changeFragment(fragmentHome);
    }

    void onShowVideo() {
        changeFragment(fragmentVideo);
    }

    void onShowFind() {
        changeFragment(fragmentFind);
    }

    void onShowMe() {
        changeFragment(fragmentMe);
    }

    void changeFragment(Fragment fragment) {
        showFragment(R.id.frag_content, fragment.getClass().getCanonicalName(), fragment);
    }

    void showFragment(int contentId, String tag, Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (getCurrentTag() != null && fragmentManager.findFragmentByTag(getCurrentTag()) != null) {
            if (tag.equals(getCurrentTag())) {
                return;
            }
            transaction.hide(fragmentManager.findFragmentByTag(getCurrentTag()));
        }

        if (fragment.isAdded()) {
            transaction.show(fragment);
        } else {
            transaction.add(contentId, fragment, tag);
        }
        setCurrentTag(tag);
        transaction.commitAllowingStateLoss();
    }

    private String getCurrentTag() {
        return currentTag;
    }

    private void setCurrentTag(String tag) {
        this.currentTag = tag;
    }

    private void resultIntent(Intent intent) {
        if (intent == null) {
            return;
        }

        int tag = intent.getIntExtra("tag", TAG_HOME);
        switch (tag) {
            case TAG_MOVIE:
                initMovie();
                break;
            case TAG_FOUND:
                initFound();
                break;
            case TAG_ME:
                initMe();
                break;
            default:
                initHome();
                break;
        }
        addFragmentIntent = null;
    }

    void initMovie() {
        rb_video.setChecked(true);
    }

    void initFound() {
        rb_find.setChecked(true);
    }

    void initMe() {
        rb_me.setChecked(true);
    }

    void initHome() {
        rb_home.setChecked(true);
    }
}
